import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Iterator;
import java.util.LinkedList;


public class LineDrawing extends Canvas implements PaintStorage{

	public void addPoint(Point ptNew) {
		lines.getLast().add(ptNew);
		addPoint2(ptNew);
	}

	
	public void clear() {
		lines.clear();
		repaint();
	}

	
	public void draw(Graphics g) {
		repaint();
	}

	public void newLine(Point ptInit, Color colLine) {
		myNewLine(ptInit, colLine, 1);
	}
	public void myNewLine(Point ptInit, Color colLine, int stroke) {
		
		lines.add(new Lines(colLine, stroke));
		lines.getLast().add(ptInit);
		myNewLine2(ptInit, colLine, stroke);
	//	li.lAdd(lines.getLast());
	}
	
	public void myDraw(){
		Graphics2D g = (Graphics2D) this.getGraphics();
		if (s > 1)
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.setStroke(new BasicStroke(s, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND));
		g.setColor(c);
		g.drawLine(x1, y1, x2, y2);
	}
	
	public LineDrawing() {
		super();
	}
	
	public void paint(Graphics g){
		Iterator<Point> i;
		Point p;
		boolean b;
		for(Lines l : lines){
			b = true;
			c = l.getColor();
			s = l.getStroke();
			i = l.iterator();
			while(i.hasNext()){
				p = i.next();
				if (b){
					b = false;
					myNewLine2(p, c, s);
				}
				else
					addPoint2(p);	
			}
			
			
		}
	}
	
	
	private void addPoint2(Point ptNew){
		x2 = ptNew.x;
		y2 = ptNew.y;
		myDraw();
		x1 = x2;
		y1 = y2;
	}

	private void myNewLine2(Point ptInit, Color colLine, int stroke) {
		x2 =ptInit.x;
		y2 =ptInit.y;
		x1 =ptInit.x;
		y1 =ptInit.y;
		s = stroke;
		c = colLine;
		myDraw();
	}

	public void undo() {
		if (lines.size() > 0)
		lines.removeLast();
		
		repaint();
	}
	
	//private Listi li;
	private int x1;
	private int x2;
	private int y1;
	private int y2;
	private Color c;
	private int s;
	private LinkedList<Lines> lines = new LinkedList<Lines>();
	private static final long serialVersionUID = 1248636080251320995L;

}

